<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img
              v-if="userInfo.user_pic"
              :src="userInfo.user_pic"
              alt=""
              class="avatar"
            />
            <img v-else src="../../assets/logo.png" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item @click="logoutHandler" index="2">
          <i class="el-icon-switch-button"></i>
          退出
        </el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px">
        <div class="user-box">
          <img v-if="userInfo.user_pic" :src="userInfo.user_pic" alt="" />
          <img v-else src="../../assets/logo.png" alt="" class="avatar" />
          <span>欢迎 {{ userInfo.nickname || userInfo.username }}</span>
        </div>
        <!-- default-active="2": 默认激活的菜单项, 值为菜单项的 index -->
        <!-- router: 开启路由模式, 开启后菜单激活时会以 index 作为 path 进行路由跳转 -->
        <el-menu
          default-active="/home"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#409EFF"
          router
        >
          <!-- 需求: 循环创建两种标签, v-for 不能写在两个标签上, 但又需要根据条件来循环创建 -->
          <!-- 解决方案: 使用一个标签包裹两者 template , 它就是一个虚拟标签, 不会在 DOM 树上生成真实的标签 -->
          <!-- template 起到的是包裹的作用, 不具备其他任何能力 -->
          <!-- 注意: template 并非真实的 DOM 元素, 所以不能绑定 key 属性, 需要将 key 绑定在其子元素上 -->
          <template v-for="item in menuList">
            <!-- index: 只能接收字符串, 是菜单项的唯一标识, 用于做菜单项高亮 -->
            <el-menu-item
              v-if="!item.children"
              :key="item.indexPath"
              :index="item.indexPath"
            >
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>

            <el-submenu v-else :key="item.indexPath" :index="item.indexPath">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>

              <el-menu-item
                v-for="ele in item.children"
                :key="ele.indexPath"
                :index="ele.indexPath"
              >
                <i :class="ele.icon"></i>
                {{ ele.title }}
              </el-menu-item>

            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
// 引入辅助函数
import { mapState } from 'vuex'

export default {
  name: 'Main',
  created () {
    this.$store.dispatch('user/getUserInfo')
    this.getMenus()
  },
  data () {
    return {
      menuList: []
    }
  },
  methods: {
    logoutHandler () {
      this.$confirm('真的要退出吗嘤嘤嘤?', '确认退出?????', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '退出登录成功呜呜呜!'
          })
          // 调用 mutation 清空 token (有持久化插件, 会自动帮我们清除 localStorage 的数据)
          this.$store.commit('user/updateToken', '')
          // 跳转至登录页
          this.$router.push('/login')
        })
        .catch((e) => e)
    },
    async getMenus () {
      // 发起请求获取菜单数据
      // const { data: res } = await this.$http.get('/my/menus', {
      //   headers: {
      //     Authorization: this.token
      //   }
      // })

      // 添加请求拦截器后不需要手动携带 token
      const { data: res } = await this.$http.get('/my/menus')
      // 判断状态
      if (res.code !== 0) return
      // 将数据存储到 data 中
      // console.log(res)
      this.menuList = res.data
    }
  },
  computed: {
    ...mapState('user', ['userInfo', 'token'])
  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
